package com.bistu.openlab.uofspreadsheet.xmlparse;

import java.io.File;

import org.dom4j.Document;
import org.dom4j.Element;

import com.bistu.openlab.uofspreadsheet.model.ExcelDocument;
import com.bistu.openlab.uofspreadsheet.util.PrepareTmpDir;
import com.bistu.openlab.uofspreadsheet.util.XmlDomHelper;
import com.bistu.openlab.zipdeal.Zip;

/*
 *@author xiaoguang.gao@oracle.com
 *@version 2013-5-9 
 */
public class ExcelDocumentParse{
	private static ExcelDocumentParse parse = new ExcelDocumentParse();
	private ExcelDocumentParse(){}
	/**
	 * 
	 * @return
	 */
	public static ExcelDocumentParse getInstance(){
		return parse;
	}
	/**
	 * read uos file to ExcelDocument
	 * @param uosFile
	 * @return
	 */
	public ExcelDocument readFromFile(File uosFile) {
		PrepareTmpDir.IniticalTmp();
		//un-compress uos file
		Zip zip = new Zip();
		zip.unZip(uosFile.getAbsolutePath(), "tmp");
		//load file
		ExcelDocument excelDoc = new ExcelDocument();
		//meta.xml
		Document metaDoc = XmlDomHelper.GetDomFromXmlFile(new File("tmp"+File.separator+"_meta"+File.separator+"meta.xml"));
		if(metaDoc!=null){
			ExcelMetaParse metaParse = ExcelMetaParse.getInstance();
			excelDoc.setExcelMeta(metaParse.readFromElement(metaDoc.getRootElement()));
		}
		//content.xml
		Document contentDoc = XmlDomHelper.GetDomFromXmlFile(new File("tmp"+File.separator+"content.xml"));
		if(contentDoc!=null){
			ExcelContentParse contentParse = ExcelContentParse.getInstance();
			excelDoc.setExcelContent(contentParse.readFromElement(contentDoc.getRootElement()));
		}
		//extend.xml
		Document extendDoc = XmlDomHelper.GetDomFromXmlFile(new File("tmp"+File.separator+"extend.xml"));
		if(extendDoc!=null){
			ExcelExtendParse extendParse = ExcelExtendParse.getInstance();
			excelDoc.setExcelExtend(extendParse.readFromElement(extendDoc.getRootElement()));
		}
		//rules.xml
		Document rulesDoc = XmlDomHelper.GetDomFromXmlFile(new File("tmp"+File.separator+"rules.xml"));
		if(rulesDoc!=null){
			ExcelRulesParse rulesParse = ExcelRulesParse.getInstance();
			excelDoc.setExcelRules(rulesParse.readFromElement(rulesDoc.getRootElement()));
		}
		//styles.xml
		Document stylesDoc = XmlDomHelper.GetDomFromXmlFile(new File("tmp"+File.separator+"styles.xml"));
		if(stylesDoc!=null){
			ExcelStylesParse stylesParse = ExcelStylesParse.getInstance();
			excelDoc.setExcelStyles(stylesParse.readFromElement(stylesDoc.getRootElement()));
		}
		//uof.xml
		Document uofDoc = XmlDomHelper.GetDomFromXmlFile(new File("tmp"+File.separator+"uof.xml"));
		if(uofDoc!=null){
			ExcelUofParse uofParse = ExcelUofParse.getInstance();
			excelDoc.setExcelUof(uofParse.readFromElement(uofDoc.getRootElement()));
		}
		return excelDoc;
	}
	/**
	 * write uos document to disk
	 * @param obj
	 * @param uosFile
	 */
	public void writeToFile(ExcelDocument obj, File uosFile) {
		//create tmp file to store xml files
		PrepareTmpDir.IniticalTmp();
		//generate xml file
		//meta.xml
		ExcelMetaParse metaParse = ExcelMetaParse.getInstance();
		Element metaEle = metaParse.writeToElement(obj.getExcelMeta());
		if(metaEle!=null){
			Document metaDoc = XmlDomHelper.GenerateDocumentByRootElement(metaEle);
			XmlDomHelper.WriteDocment(metaDoc, new File("tmp"+File.separator+"_meta"+File.separator+"meta.xml"));
		}
		//content.xml
		ExcelContentParse contentParse = ExcelContentParse.getInstance();
		Element contentEle = contentParse.writeToElement(obj.getExcelContent());
		if(contentEle!=null){
			Document contentDoc = XmlDomHelper.GenerateDocumentByRootElement(contentEle);
			XmlDomHelper.WriteDocment(contentDoc, new File("tmp"+File.separator+"content.xml"));
		}
		//extend.xml
		ExcelExtendParse extendParse = ExcelExtendParse.getInstance();
		Element extendEle = extendParse.writeToElement(obj.getExcelExtend());
		if(extendEle!=null){
			Document extendDoc = XmlDomHelper.GenerateDocumentByRootElement(extendEle);
			XmlDomHelper.WriteDocment(extendDoc, new File("tmp"+File.separator+"extend.xml"));
		}
		//rules.xml
		ExcelRulesParse rulesParse = ExcelRulesParse.getInstance();
		Element rulesEle = rulesParse.writeToElement(obj.getExcelRules());
		if(rulesEle!=null){
			Document rulesDoc = XmlDomHelper.GenerateDocumentByRootElement(rulesEle);
			XmlDomHelper.WriteDocment(rulesDoc, new File("tmp"+File.separator+"rules.xml"));
		}
		//styles.xml
		ExcelStylesParse stylesParse = ExcelStylesParse.getInstance();
		Element stylesEle = stylesParse.writeToElement(obj.getExcelStyles());
		if(stylesEle!=null){
			Document stylesDoc = XmlDomHelper.GenerateDocumentByRootElement(stylesEle);
			XmlDomHelper.WriteDocment(stylesDoc, new File("tmp"+File.separator+"styles.xml"));
		}
		//uof.xml
		ExcelUofParse uofParse = ExcelUofParse.getInstance();
		Element uofEle = uofParse.writeToElement(obj.getExcelUof());
		if(uofEle!=null){
			Document uofDoc = XmlDomHelper.GenerateDocumentByRootElement(uofEle);
			XmlDomHelper.WriteDocment(uofDoc, new File("tmp"+File.separator+"uof.xml"));
		}
		//compress to uos file
		Zip zip = new Zip();
		zip.doZip("tmp", uosFile.getAbsolutePath());
		//clear tmp directory
		PrepareTmpDir.ClearTmp();
	}

}
